﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Reflection;
using System.Web.UI.WebControls;
/// <summary>
///AutoForm 的摘要说明
/// </summary>
public class AutoForm
{
    public static IEnumerable<Control> GetFormContrls<T>(object value)
    {
        foreach (PropertyInfo pinfo in typeof(T).GetProperties().Where(x=>!x.PropertyType.IsGenericType))
        {
            //HttpContext.Current.Response.Write(pinfo.PropertyType.IsEnum+"<br>");

            IAutoForm inter = AutoFormFactory.GetAutoFormCreator(pinfo);

            foreach (Control c in inter.GetControls(pinfo,value))
            {
                yield return c;
            }
        }
    }

    public static T SetValue<T>(T value)
    {
        foreach (PropertyInfo pinfo in typeof(T).GetProperties().Where(x=>!x.PropertyType.IsGenericType))
        {
            string thisV=HttpContext.Current.Request.Form[pinfo.Name];

            IAutoForm inter = AutoFormFactory.GetAutoFormCreator(pinfo);            

            try
            {
                pinfo.SetValue(value, inter.GetValue(pinfo, thisV), null);                
            }
            catch (Exception ex)
            {
                //HttpContext.Current.Response.Write(pinfo.Name+"<br>");
            }
        }

        return value;
    }
}
